package com.whut.monitor.controller;

import com.whut.monitor.vo.Tree;
import com.whut.monitor.model.SensorEntity;
import com.whut.monitor.service.ISensorService;
import com.whut.monitor.vo.ComboVo;
import com.whut.monitor.vo.SensorVo;
import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Sandeepin
 * 传感器管理接口
 */
@Controller
@RequestMapping("/sensor")
public class SensorController {

	@Autowired
	private ISensorService sensorService;

	@RequestMapping("/manager")
	public String manager() {
		return "/mintor/basicInformation/sensor";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(SensorVo sensor, PageFilter pageFilter) {
		Grid grid = new Grid();
		grid.setRows(sensorService.dataGrid(sensor, pageFilter));
		grid.setTotal(sensorService.count(sensor, pageFilter));
		return grid;
	}

	@RequestMapping("/add")
	@ResponseBody
	public ResponseMsg add(SensorVo sensorVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			sensorService.add(sensorVo);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMsg edit(SensorVo sensorVo){
		ResponseMsg j = new ResponseMsg();
		try {
			sensorService.edit(sensorVo);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public ResponseMsg delete(Integer id) {
		ResponseMsg j = new ResponseMsg();
		try {
			sensorService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree() {
		return sensorService.tree();
	}

	@RequestMapping("/getAllSensor")
	@ResponseBody
	public List<ComboVo> getAllSensor(Integer channelId) {
		List<ComboVo> list=new ArrayList<ComboVo>();
		List<SensorEntity> entity=sensorService.getAllSensor();
		for (SensorEntity sensorEntity : entity) {
			ComboVo comboVo=new ComboVo();
			comboVo.setValue(sensorEntity.getId());
			comboVo.setText(sensorEntity.getSensorName());
			comboVo.setNum(sensorEntity.getSensorNum());
			list.add(comboVo);
		}
		return list;
	}
}

